﻿//-----------------------------------------------------------------------
// <copyright file="FormDefault.cs" company="Panchal Softwares">
//     Copyright (c) Panchal Softwares. All rights reserved.
// </copyright>
// <author>Karn Panchal</author>
//-----------------------------------------------------------------------

/// <summary>
/// Namespace Text Finder GUI.
/// </summary>
namespace PanchalSoftwares.TextFinder.GUI
{
    using System;
    using System.IO;
    using System.Windows.Forms;
    using Properties;
    using PSTLFH = PanchalSoftwares.TextFinder.Library.FileHelper;

    /// <summary>
    /// Partial Class Form Default.
    /// </summary>
    public partial class FormDefault : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="FormDefault"/> class.
        /// </summary>
        public FormDefault()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Enumerated Status.
        /// </summary>
        private enum Status
        {
            /// <summary>
            /// The success
            /// </summary>
            Success = 0,

            /// <summary>
            /// The warning
            /// </summary>
            Warning = 1,

            /// <summary>
            /// The error
            /// </summary>
            Error = 2
        }

        /// <summary>
        /// Handles the Load event of the FormDefault control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void FormDefault_Load(object sender, EventArgs e)
        {
            try
            {
                FormDefault.CheckForIllegalCrossThreadCalls = false;

                this.SetGUIStatus(Status.Success, Resources.AppLoadSuccess);
            }
            catch (Exception ex)
            {
                this.SetGUIStatus(Status.Error, ex.Message);
            }
        }

        /// <summary>
        /// Handles the Click event of the ExitToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        /// <summary>
        /// Handles the Click event of the AboutToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            AboutBoxDefault item = new AboutBoxDefault();
            if (item.ShowDialog() == System.Windows.Forms.DialogResult.OK || true)
            {
                this.Enabled = true;
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonRootLocation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ButtonRootLocation_Click(object sender, EventArgs e)
        {
            if (this.folderBrowserDialogRootLocation.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                this.textBoxRootLocation.Text = this.folderBrowserDialogRootLocation.SelectedPath;
                this.backgroundWorkerRootLocation.RunWorkerAsync();
            }
        }

        /// <summary>
        /// Handles the Click event of the ButtonSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void ButtonSearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.textBoxSearchText.Text != string.Empty)
                {
                    this.backgroundWorkerSearch.RunWorkerAsync();
                }
                else
                {
                    this.SetGUIStatus(Status.Warning, Resources.EnterSearchText);
                }
            }
            catch (Exception ex)
            {
                this.SetGUIStatus(Status.Error, ex.Message);
            }
        }

        /// <summary>
        /// Sets the GUI status.
        /// </summary>
        /// <param name="status">The status.</param>
        /// <param name="message">The message.</param>
        private void SetGUIStatus(Status status, string message)
        {
            switch (status)
            {
                case Status.Success:
                    this.toolStripStatusLabelImage.Image = Properties.Resources.Success;
                    this.toolStripStatusLabelText.Text = message;
                    break;
                case Status.Warning:
                    this.toolStripStatusLabelImage.Image = Properties.Resources.Information;
                    this.toolStripStatusLabelText.Text = message;
                    break;
                case Status.Error:
                    this.toolStripStatusLabelImage.Image = Properties.Resources.Exclamation;
                    this.toolStripStatusLabelText.Text = message;
                    break;
            }
        }

        /// <summary>
        /// Handles the DoWork event of the BackgroundWorkerRootLocation control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        private void BackgroundWorkerRootLocation_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            if (Directory.Exists(this.textBoxRootLocation.Text))
            {
                if (PSTLFH.IsAllDirectoriesAccessible(this.textBoxRootLocation.Text))
                {
                    this.buttonSearch.Enabled = true;
                    this.textBoxSearchText.Enabled = true;
                    this.SetGUIStatus(Status.Success, Resources.DirAccessSuccess);
                }
                else
                {
                    this.buttonSearch.Enabled = false;
                    this.textBoxSearchText.Enabled = false;
                    this.SetGUIStatus(Status.Warning, Resources.DirAccessError);
                }
            }

            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// Handles the DoWork event of the BackgroundWorkerSearch control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.ComponentModel.DoWorkEventArgs"/> instance containing the event data.</param>
        private void BackgroundWorkerSearch_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            this.Cursor = Cursors.WaitCursor;
            this.listBoxResultFiles.Items.Clear();
            Settings item = new Settings();
            PSTLFH.UseCaseSensitiveSearch = item.CaseSensetiveSearch;
            foreach (FileInfo fileInfo in PSTLFH.FindFiles(this.textBoxRootLocation.Text, "txt", this.textBoxSearchText.Text))
            {
                if (!this.listBoxResultFiles.Items.Contains(fileInfo.FullName))
                {
                    this.listBoxResultFiles.Items.Add(fileInfo.FullName);
                }
            }

            if (this.listBoxResultFiles.Items.Count > 0)
            {
                this.SetGUIStatus(Status.Success, string.Format(Resources.FilesFound, this.listBoxResultFiles.Items.Count));
            }
            else
            {
                this.SetGUIStatus(Status.Warning, string.Format(Resources.NoFilesFound, this.textBoxSearchText.Text));
            }

            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// Handles the Click event of the SettingsToolStripMenuItem control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        private void SettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Enabled = false;
            FormSettings item = new FormSettings();
            if (item.ShowDialog() == System.Windows.Forms.DialogResult.OK || true)
            {
                this.Enabled = true;
            }
        }
    }
}
